package com.cswnode.demo.controller;

import com.cswnode.demo.entity.Message;
import com.cswnode.demo.entity.User;
import com.cswnode.demo.service.ChatMsgService;
import com.cswnode.demo.service.MessageService;
import com.cswnode.demo.service.UserService;
import com.cswnode.demo.util.ResponseVo;
import com.cswnode.demo.util.Utils;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author csw
 * @create 2021/3/27 17:53
 */
@RestController
@RequestMapping("/chat")
public class ChatController {

    @Autowired
    private UserService userService;
    @Autowired
    private MessageService messageService;
    @Autowired
    private ChatMsgService chatMsgService;

    @ApiOperation(value = "获得所有用户")
    @GetMapping("/admin")
    public ResponseVo getAllUsers(String username){
        if (StringUtils.isEmpty(username)){
            List<User> users = userService.queryChatMsg();
            return ResponseVo.success(users);
        }
        return ResponseVo.success(userService.selectAll(username));
    }

    @ApiOperation(value = "修改")
    @PostMapping("/set")
    public ResponseVo set(@RequestBody String username){
        Message message = new Message();
        message.setMessage(username);
        message.setUserId(Utils.getLoginUserId());
        messageService.update(message);
        return ResponseVo.success();
    }

    @ApiOperation(value = "获得")
    @PostMapping("/get")
    public ResponseVo get(){
        Message message = messageService.get();
        if (message != null){
            return ResponseVo.success(message.getMessage());
        }
        return ResponseVo.success("");

    }




}
